using System;
using System.Collections.Generic;
using System.Text;
using MCGameEngine.Animations;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Content;

namespace MCGameEngine.Physic_Layer
{
    public class AnimatedObject : PhysicObject
    {
        AnimationPlayer animationPlayer;
        SkinningData skinningData;

        public override Model Model
        {
            get
            {
                return base.Model;
            }

            set
            {
                skinningData = value.Tag as SkinningData;
                animationPlayer = new AnimationPlayer(skinningData);
                base.Model = value;
            }
        }

        public void StartClip(string movieclip)
        {
            if (skinningData != null)
            {
                AnimationClip clip = skinningData.AnimationClips[movieclip];
                animationPlayer.StartClip(clip);
            }
        }

        public override void Update(GameTime gameTime)
        {
            animationPlayer.Update(gameTime.ElapsedGameTime, true,World);
            base.Update(gameTime);
        }

        public override void Draw(Camera camera)
        {
            if (this.Model == null)
            {
                return;
            }

            Matrix[] trasform = animationPlayer.GetSkinTransforms();


                foreach (ModelMesh mesh in Model.Meshes)
                {
                    foreach (Effect effect in mesh.Effects)
                    {
                        effect.Parameters["Bones"].SetValue(trasform);
                        effect.Parameters["View"].SetValue(camera.View);
                        effect.Parameters["Projection"].SetValue(camera.Projection);
                    }
                    mesh.Draw();
                }
        }
    }
}
